﻿using Apps.Common;
using Apps.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.BLL.LMS
{
    public partial class LMS_COLLECTSAMPLEBLL
    {
        public DBContainer db = new DBContainer();

        public List<LMS_COLLECTSAMPLEModel> GetList(ref GridPager pager, string TEST_CODE, string startdate, string enddate, string type,string COLLECT_ID, string SAMPLE_ID, string MAKE_CODE, string BATCH_CODE)
        {
            IQueryable<LMS_COLLECTSAMPLE> queryData = this.db.LMS_COLLECTSAMPLE.AsQueryable<LMS_COLLECTSAMPLE>();
            //IQueryable<V_PL_ALL_DETAILS> allData = this.db.V_PL_ALL_DETAILS.Where(a=>a.BATCH_TYPE.Contains(type)).AsQueryable<V_PL_ALL_DETAILS>();
            if (!string.IsNullOrEmpty(TEST_CODE))
            {
                queryData = queryData.Where(m => m.TEST_CODE.Contains(TEST_CODE));
            }
            if (!string.IsNullOrEmpty(COLLECT_ID))
            {
                queryData = queryData.Where(m => m.S_SAMPLE.COLLECT_CODE.Contains(COLLECT_ID));
            }
            if (!string.IsNullOrEmpty(SAMPLE_ID))
            {
                queryData = queryData.Where(m => m.S_SAMPLE.SAMPLE_CODE.Contains(SAMPLE_ID));
            }
            if (!string.IsNullOrEmpty(MAKE_CODE))
            {
                queryData = queryData.Where(m => m.MAKE_CODE.Contains(MAKE_CODE));
            }
            if (!string.IsNullOrEmpty(BATCH_CODE))
            {
                queryData = queryData.Where(m => m.BATCH_CODE.Contains(BATCH_CODE));
            }
            if (!string.IsNullOrEmpty(startdate))
            {
                DateTime dStartDate = DateTime.ParseExact(startdate, "yyyy-MM-dd", null);
                queryData = queryData.Where(m => m.COLLECT_TIME >= dStartDate);
            }
            if (!string.IsNullOrEmpty(enddate))
            {
                DateTime dEndDate = DateTime.ParseExact(enddate, "yyyy-MM-dd", null).AddDays(1);
                queryData = queryData.Where(m => m.COLLECT_TIME < dEndDate);
            }
            var data = from a1 in queryData
                       join m1 in db.V_PL_ALL_DETAILS on a1.TEST_CODE equals m1.TEST_CODE
                       join m3 in db.SysUser on a1.COLLECT_USER_ID equals m3.Id into l3
                       from l33 in l3.DefaultIfEmpty() 
                       join m4 in db.SysUser on a1.SEND_USER_ID equals m4.Id into l4
                       from l44 in l4.DefaultIfEmpty()
                       where m1.BATCH_TYPE.Contains(type) && !string.IsNullOrEmpty(a1.TEST_CODE)
                       select new LMS_COLLECTSAMPLEModel()
                       {
                           BATCH_CODE = a1.BATCH_CODE,
                           //COLLECT_ID = m1.COLLECT_ID,
                           COLLECT_TIME = a1.COLLECT_TIME,
                           COLLECT_USER_ID = a1.COLLECT_USER_ID,
                           COLLECT_USER_NAME = l33.TrueName,
                           MAKE_CODE = a1.MAKE_CODE,
                           MAKE_TYPE = a1.MAKE_TYPE,
                           PRINT_COUNT = a1.PRINT_COUNT,
                           SAMPLE_CODE = a1.S_SAMPLE.SAMPLE_CODE,
                           COLLECTION_ID= a1.S_SAMPLE.COLLECT_CODE,
                           SAMPLE_TYPE = a1.S_SAMPLE.SAMPLE_TYPE,
                           SAMPLE_ID = a1.SAMPLE_ID,
                           SEND_USER_ID = a1.SEND_USER_ID,
                           SEND_USER_NAME = l44.TrueName,
                           TEST_CODE = m1.TEST_CODE,
                           WEIGHT = a1.S_SAMPLE.WEIGHT
                       };


                           

            pager.totalRows = data.Count();
            //排序
            data = LinqHelper.SortingAndPaging(data, pager.sort, pager.order, pager.page, pager.rows);
            var retval = data.ToList();
            return retval;
        }
    }
}
